// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0

package general

import (
	"context"
	"fmt"

	"github.com/go-logr/logr"
	appsv1 "k8s.io/api/apps/v1"
	apierrors "k8s.io/apimachinery/pkg/api/errors"
	"k8s.io/apimachinery/pkg/types"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/log"

	"github.com/gardener/gardener/extensions/pkg/controller/healthcheck"
	gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
	"github.com/gardener/gardener/pkg/utils/kubernetes/health"
)

// statefulSetHealthChecker contains all the information for the StatefulSet HealthCheck
type statefulSetHealthChecker struct {
	logger logr.Logger
	client client.Client
	name   string
}

// SeedStatefulSetHealthChecker is a healthCheck for StatefulSets in the Seed cluster
type SeedStatefulSetHealthChecker struct {
	statefulSetHealthChecker
}

// ShootStatefulSetHealthChecker is a healthCheck for StatefulSets in the Shoot cluster
type ShootStatefulSetHealthChecker struct {
	statefulSetHealthChecker
}

var (
	_ healthcheck.HealthCheck = (*SeedStatefulSetHealthChecker)(nil)
	_ healthcheck.SeedClient  = (*SeedStatefulSetHealthChecker)(nil)
	_ healthcheck.HealthCheck = (*ShootStatefulSetHealthChecker)(nil)
	_ healthcheck.ShootClient = (*ShootStatefulSetHealthChecker)(nil)
)

// NewSeedStatefulSetChecker is a healthCheck function to check StatefulSets in the Seed cluster
func NewSeedStatefulSetChecker(name string) *SeedStatefulSetHealthChecker {
	return &SeedStatefulSetHealthChecker{
		statefulSetHealthChecker: statefulSetHealthChecker{
			name: name,
		},
	}
}

// NewShootStatefulSetChecker is a healthCheck function to check StatefulSets in the Shoot cluster
func NewShootStatefulSetChecker(name string) *ShootStatefulSetHealthChecker {
	return &ShootStatefulSetHealthChecker{
		statefulSetHealthChecker: statefulSetHealthChecker{
			name: name,
		},
	}
}

// InjectSeedClient injects the seed client
func (h *SeedStatefulSetHealthChecker) InjectSeedClient(seedClient client.Client) {
	h.client = seedClient
}

// InjectShootClient injects the shoot client
func (h *ShootStatefulSetHealthChecker) InjectShootClient(shootClient client.Client) {
	h.client = shootClient
}

// SetLoggerSuffix injects the logger
func (h *statefulSetHealthChecker) SetLoggerSuffix(provider, extension string) {
	h.logger = log.Log.WithName(fmt.Sprintf("%s-%s-healthcheck-statefulset", provider, extension))
}

// Check executes the health check
func (h *statefulSetHealthChecker) Check(ctx context.Context, request types.NamespacedName) (*healthcheck.SingleCheckResult, error) {
	statefulSet := &appsv1.StatefulSet{}

	if err := h.client.Get(ctx, client.ObjectKey{Namespace: request.Namespace, Name: h.name}, statefulSet); err != nil {
		if apierrors.IsNotFound(err) {
			return &healthcheck.SingleCheckResult{
				Status: gardencorev1beta1.ConditionFalse,
				Detail: fmt.Sprintf("StatefulSet %q in namespace %q not found", h.name, request.Namespace),
			}, nil
		}
		err := fmt.Errorf("failed to retrieve StatefulSet %q in namespace %q: %w", h.name, request.Namespace, err)
		h.logger.Error(err, "Health check failed")
		return nil, err
	}
	if isHealthy, err := statefulSetIsHealthy(statefulSet); !isHealthy {
		h.logger.Error(err, "Health check failed")
		return &healthcheck.SingleCheckResult{
			Status: gardencorev1beta1.ConditionFalse,
			Detail: err.Error(),
		}, nil
	}

	return &healthcheck.SingleCheckResult{
		Status: gardencorev1beta1.ConditionTrue,
	}, nil
}

func statefulSetIsHealthy(statefulSet *appsv1.StatefulSet) (bool, error) {
	if err := health.CheckStatefulSet(statefulSet); err != nil {
		err := fmt.Errorf("statefulSet %q in namespace %q is unhealthy: %w", statefulSet.Name, statefulSet.Namespace, err)
		return false, err
	}
	return true, nil
}
